package winsplitter;

import java.awt.GridBagLayout;
import javax.swing.JPanel;
import java.awt.GridBagConstraints;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import java.awt.Dimension;

import javax.swing.JFileChooser;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import wcg.FileDropTable;
import wcg.WinExporter;

public class PanelwinSplitter extends JPanel implements ActionListener {

	private static final long serialVersionUID = 1L;
	private JPanel jPanelNorth = null;
	private JButton jButtonDoSplit = null;
	private JPanel jPanelCenter = null;
	private JLabel jLabelUsage = null;
	private winsplitter WinSpliiter=null;
	private WinExporter WinConverter=null;
	public String getOutDir() {
		return OutDir;
	}

	public void setOutDir(String outDir) {
		OutDir = outDir;
	}

	private String OutDir = new String("");  //  @jve:decl-index=0:
	private JComboBox jComboBoxOutputLength = null;
	private JScrollPane jScrollPane = null;
	private FileDropTable jTableFiles = null;
	/**
	 * This is the default constructor
	 */
	public PanelwinSplitter() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(600, 220);
		this.setLayout(new BorderLayout());
		this.add(getJPanelNorth(), BorderLayout.NORTH);
		this.add(getJPanelCenter(), BorderLayout.CENTER);
	}

	/**
	 * This method initializes jPanelNorth	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelNorth() {
		if (jPanelNorth == null) {
			jPanelNorth = new JPanel();
			jPanelNorth.setLayout(new BorderLayout());
			jPanelNorth.add(getJScrollPane(), BorderLayout.NORTH);  // Generated
		}
		return jPanelNorth;
	}

	/**
	 * This method initializes jButtonDoSplit	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonDoSplit() {
		if (jButtonDoSplit == null) {
			jButtonDoSplit = new JButton();
			jButtonDoSplit.setText("Do split");
			jButtonDoSplit.setBounds(new Rectangle(299, 15, 97, 26));
			jButtonDoSplit.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if(outDirectorySelect()){
						WinSpliiter= new winsplitter();
						WinSpliiter.setOutDir(OutDir);
						int lengthcode = jComboBoxOutputLength.getSelectedIndex();
						// 0: 1MIN, 1: 1HOUR
						WinSpliiter.setLengthcode(lengthcode);
						
						Vector<String> fn = jTableFiles.getFileNames();
						System.out.println("---------------------------");
						System.out.println(fn.size() + " input files");
						Iterator<String> ite = fn.iterator();
						while(ite.hasNext()){
							String s = ite.next();
							WinSpliiter.Split(s);
							WinSpliiter.clear();
						}
						
//						String files = jTextFieldFiles.getText();
//						String [] f = files.split(";");
//						for(int i=0;i<f.length;i++){
//							WinSpliiter.Split(f[i]);
//						}
					}
				}
			});
		}
		return jButtonDoSplit;
	}

	/**
	 * This method initializes jPanelCenter	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelCenter() {
		if (jPanelCenter == null) {
			jLabelUsage = new JLabel();
			jLabelUsage.setBounds(new Rectangle(9, 15, 166, 28));
			jLabelUsage.setText("Split WIN file into WIN files");
			jPanelCenter = new JPanel();
			jPanelCenter.setLayout(null);
			jPanelCenter.add(getJButtonDoSplit(), null);
			jPanelCenter.add(jLabelUsage, null);
			jPanelCenter.add(getJComboBoxOutputLength(), null);
		}
		return jPanelCenter;
	}
	///
	private boolean outDirectorySelect(){
		if(OutDir.isEmpty()){
			Vector<String> fn = jTableFiles.getFileNames();
			String filepath = fn.get(0);
			if(filepath!=null){
				File f = new File(filepath);
				String path=f.getAbsolutePath();
				if(path!=null){
					OutDir=path;
				}
			}
		}
		String initPath = new String(OutDir);	
		//JFileChooser filechooser = new JFileChooser(""); //@jve:decl-index=0
		JFileChooser filechooser = new JFileChooser(initPath); //@jve:decl-index=0
		filechooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );	      
		filechooser.setDialogTitle("Save folder");
		int selected = filechooser.showSaveDialog(this);
		if ( selected == JFileChooser.APPROVE_OPTION ) {
			File file = filechooser.getSelectedFile ();
			String dirPath = file.getAbsolutePath();
			OutDir = new String(dirPath);
			return true;
		}
		return false;
	}

	/**
	 * This method initializes jComboBoxOutputLength	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBoxOutputLength() {
		if (jComboBoxOutputLength == null) {
			jComboBoxOutputLength = new JComboBox();
			jComboBoxOutputLength.setBounds(new Rectangle(181, 15, 110, 25));
			jComboBoxOutputLength.addItem("1 minute WIN");
			jComboBoxOutputLength.addItem("1 hour WIN");
			jComboBoxOutputLength.setSelectedIndex(0);
			jComboBoxOutputLength.addActionListener(this);
		}
		return jComboBoxOutputLength;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setPreferredSize(new Dimension(200,150));
			jScrollPane.setViewportView(getJTableFiles());  // Generated
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTableFiles	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private FileDropTable getJTableFiles() {
		if (jTableFiles == null) {
			jTableFiles = new FileDropTable();
		}
		return jTableFiles;
	}

}  //  @jve:decl-index=0:visual-constraint="81,42"
